<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/3
 * Time: 14:06
 */
namespace app\first\model;

use think\Model;

class Message extends Model{
    protected $autoWriteTimestamp = true;
    /**
     * @param array $where
     * @param string $order
     * @param int $limit
     * @param string $filed
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='id desc',$limit=10,$filed='*'){
         $list = $this
            ->field($filed)
            ->with(['user'=>function($query){
                $query->field('id,username,nickname,sex,head,rong_token');
            },'article'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        return $list;
    }

    public function user(){
        return $this->hasOne('member','id','mid');
    }

    public function article(){
        return $this->hasOne('article','id','aid');
    }

    public function message($data,&$out=''){
        $valid = new \app\first\validate\Message();
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        if(!$this->insert($data)){
            $out = '操作失败';
            return false;
        }
        return true;
    }
}